#include "virtual_pic8259a.h"

// 初始化PIC
void PIC_INIT(struct PIC8259A* pic) {
    pic->imr = 0xFF; // 初始时屏蔽所有中断
    pic->irr = 0x00;
    pic->isr = 0x00;
}
// 向PIC发送初始化命令字（ICW）
void PIC_writeICW(struct PIC8259A* pic, int icw_index, unsigned char value) {
    pic->icw[icw_index] = value;
}

// 向8259A写入操作命令字
void PIC_writeOCW(struct PIC8259A* pic, int ocw_index, unsigned char value) {
    if (0 == ocw_index) {
        pic->ocw[ocw_index] = value;
        pic->imr = value;
    } else {
        pic->ocw[ocw_index] = value;
    }
}

bool IS_BIT_SET(unsigned char data, int bit_position){
    if (data & (1 << bit_position)) {
        return true;
    } else {
        return false;
    }
}
void SET_BIT(unsigned char data, int bit_position) {
    data |= (1 << bit_position);
}

void CLEAR_BIT(struct PIC8259A* pic, int bit_position) {
    (pic->irr) &= ~(1 << bit_position);
}

int PIC_PR(struct PIC8259A* pic) {
    int i = 0;
    while(1) {
        if (IS_BIT_SET(pic->irr, i)) {
            return i;
        };
        i++;
    };
}